/**
 * 添加或修改URL参数
 * @param params 相关对象字段
 */
export function updateUrlParams(params) {
  if (window.history.pushState) {
    const url = new URL(window.location.href)
    // 设置新参数或修改已有参数
    Object.keys(params).forEach((key) => {
      if (params[key]) {
        url.searchParams.set(key, params[key])
      }
      else {
        url.searchParams.delete(key)
      }
    })
    // 不刷新页面修改历史记录
    window.history.pushState({ path: url.href }, '', url.href)
  }
}

/**
 * 搜索后回显带颜色字符
 * @param {string} queryString - 搜索条件
 * @param {Array} list - 列表
 * @param {string} key - 查询的key
 * @returns
 */
export function handleInputReplace(queryString, list, key) {
  if (!queryString) return list
  const reg = new RegExp(`(${queryString})`)
  return list.reduce((res, cur) => {
    const item = cur
    if (reg.test(item[key])) {
      item[key] = item[key].replace(reg, '<span style="color:#F53F3F">$&</span>')
      res.push({ ...item })
    }
    return res
  }, [])
}

/**
 * 构造树型结构数据
 * @param {*} data 数据源
 * @param {*} id id字段 默认 'id'
 * @param {*} parentId 父节点字段 默认 'parentId'
 * @param {*} children 孩子节点字段 默认 'children'
 */
export function handleTree(data, id, parentId, children) {
  const config = {
    id: id || 'id',
    parentId: parentId || 'parentId',
    childrenList: children || 'children',
  }

  const childrenListMap = {}
  const nodeIds = {}
  const tree = []

  for (const d of data) {
    const parentId = d[config.parentId]
    if (childrenListMap[parentId] == null) {
      childrenListMap[parentId] = []
    }
    nodeIds[d[config.id]] = d
    childrenListMap[parentId].push(d)
  }

  for (const d of data) {
    const parentId = d[config.parentId]
    if (nodeIds[parentId] == null) {
      tree.push(d)
    }
  }

  for (const t of tree) {
    adaptToChildrenList(t)
  }

  function adaptToChildrenList(o) {
    if (childrenListMap[o[config.id]] !== null) {
      o[config.childrenList] = childrenListMap[o[config.id]]
    }
    if (o[config.childrenList]) {
      for (const c of o[config.childrenList]) {
        adaptToChildrenList(c)
      }
    }
  }
  return tree
}

/**
 * 拆分树型结构数据
 * @param {Array} arrs 树形数据
 * @param {string} childs 树形数据子数据的属性名,常用'children'
 * @param {boolean} flag 是否排除 childs 参数
 * @param {Array} attrArr 需要提取的公共属性数组(默认是除了childs的全部属性)
 * @returns
 */
export function extractTree(arrs, childs, flag = false, attrArr?: any[]) {
  let attrList = []
  if (!Array.isArray(arrs) && !arrs.length)
    return []
  if (typeof childs !== 'string')
    return []
  if (!Array.isArray(attrArr) || (Array.isArray(attrArr) && !attrArr.length)) {
    attrList = Object.keys(arrs[0])
    flag && attrList.splice(attrList.indexOf(childs), 1)
  }
  else {
    attrList = attrArr
  }
  const list = []
  const getObj = (arr) => {
    arr.forEach((row) => {
      const obj = {}
      !flag && (obj[childs] = [])
      attrList.forEach((item) => {
        obj[item] = row[item]
      })
      list.push(obj)
      if (row[childs]) {
        getObj(row[childs])
      }
    })
    return list
  }
  return getObj(arrs)
}

/**
 * 数组对象去重
 * @param list 数组对象
 * @param value 根据字符串匹配
 * @returns
 */
export function uniqueFun(list, value) {
  const arr = list.reduce((prev, cur) => {
    const ids = prev.map(obj => obj[value])
    if (!ids.includes(cur[value])) {
      prev.push(cur)
    }
    return prev
  }, [])
  return arr
}
